﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Windows.Forms;

namespace BUS
{
    public class CUndoRedo
    {
        private ArrayList _Undo;
        private int _Ma;

        private class OneUndo
        {
            private string _Text;

            public string Text
            {
              get { return _Text; }
              set { _Text = value; }
            }
                        private int _Start;

            public int Start
            {
              get { return _Start; }
              set { _Start = value; }
            }
            private int _Length;

            public int Length
            {
                get { return _Length; }
                set { _Length = value; }
            }

            public OneUndo(string Text, int Start, int Leangh)
            {
                _Text = Text;
                _Start = Start;
                _Length = Leangh;
            }
        }

        public CUndoRedo()
        {
            _Undo = new ArrayList();
            _Ma = 0;
        }

        public void Install(ref RichTextBox richTextBox)
        {
            richTextBox.KeyDown += new KeyEventHandler(KeyDownUp);
            richTextBox.KeyUp += new KeyEventHandler(KeyDownUp);
            _Undo.Add(new OneUndo(richTextBox.Text, richTextBox.SelectionStart, richTextBox.SelectionLength));
            _Ma = 1;
        }

        void KeyDownUp(object sender, KeyEventArgs e)
        {
            RichTextBox _temp = (RichTextBox)sender;
            OneUndo _tempUndo = (OneUndo)_Undo[_Ma-1];
            if (_temp.Text != _tempUndo.Text)
            {
                if (_Ma < _Undo.Count)
                    _Undo.RemoveRange(_Ma, _Undo.Count - _Ma);
                _Undo.Add(new OneUndo(_temp.Text, _temp.SelectionStart, _temp.SelectionLength));
                _Ma++;
            }
        }

        public void Reset(RichTextBox richTextBox)
        {
            _Undo.Clear();
            _Undo.Add(new OneUndo(richTextBox.Text, richTextBox.SelectionStart, richTextBox.SelectionLength));
            _Ma = 1;
        }

        public void Undo(ref RichTextBox richTextBox)
        {
            if (_Ma == _Undo.Count)
            {
                OneUndo _tempUndo = (OneUndo)_Undo[_Ma - 1];
                if (richTextBox.Text != _tempUndo.Text)
                {
                    if (_Ma < _Undo.Count)
                        _Undo.RemoveRange(_Ma, _Undo.Count - _Ma);
                    _Undo.Add(new OneUndo(richTextBox.Text, richTextBox.SelectionStart, richTextBox.SelectionLength));
                    _Ma++;
                }
            }
            if(_Ma > 1)
            {
                _Ma--;
                OneUndo _temp=(OneUndo)_Undo[_Ma-1];
                richTextBox.Text = _temp.Text;
                richTextBox.SelectionStart = _temp.Start;
                richTextBox.SelectionLength = _temp.Length;
            }
        }

        public void Redo(ref RichTextBox richTextBox)
        {
            if (_Ma < _Undo.Count)
            {
                OneUndo _temp = (OneUndo)_Undo[_Ma];
                richTextBox.Text = _temp.Text;
                richTextBox.SelectionStart = _temp.Start;
                richTextBox.SelectionLength = _temp.Length;
                _Ma++;
            }
        }
    }
}
